/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.Customer;
import com.chlitina.store.modules.base.entity.CustomerGroup;
import com.chlitina.store.modules.base.entity.CustomerGroupDetail;
import com.chlitina.store.modules.base.service.CustomerGroupDetailService;
import com.chlitina.store.modules.base.service.CustomerGroupService;
import com.chlitina.store.modules.base.service.CustomerService;

/**
 * 客户组详情Controller
 * @author lvbo
 * @version 2013-07-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/customerGroupDetail")
public class CustomerGroupDetailController extends BaseController {

	@Autowired
	private CustomerGroupDetailService customerGroupDetailService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private CustomerGroupService customerGroupService;
	
	@ModelAttribute
	public CustomerGroupDetail get(@RequestParam(required=false) Long id) {
		return new CustomerGroupDetail();
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(CustomerGroupDetail customerGroupDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		String groupId = request.getParameter("groupId");
        Page<CustomerGroupDetail> page = customerGroupDetailService.find(new Page<CustomerGroupDetail>(request, response), customerGroupDetail, groupId); 
        model.addAttribute("groupId", groupId);
        model.addAttribute("page", page);
		return "modules/base/customerGroupDetailList";
	}
	@RequestMapping(value = "detail")
	public String detail(CustomerGroupDetail customerGroupDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		String groupId = request.getParameter("groupId");
		Page<CustomerGroupDetail> page = customerGroupDetailService.find(new Page<CustomerGroupDetail>(request, response), customerGroupDetail, groupId); 
		model.addAttribute("groupId", groupId);
		model.addAttribute("page", page);
		return "modules/base/customerGroupDetailDetail";
	}

	@RequestMapping(value = "form")
	public String form(@RequestParam("groupId") Long groupId, CustomerGroupDetail customerGroupDetail, Model model) {
		CustomerGroup customerGroup = customerGroupService.get(groupId);
		model.addAttribute("customerGroup", customerGroup);
		return "modules/base/customerGroupDetailForm";
	}

	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, CustomerGroupDetail customerGroupDetail, Model model, RedirectAttributes redirectAttributes) {
		String groupId = request.getParameter("groupId");
		Long id = Long.parseLong(groupId);
		CustomerGroup customerGroup = customerGroupService.get(id);
		String customerIds = request.getParameter("customerIds");
		int index = 0;
		if (customerIds != null && !"".equals(customerIds)) {
			String[] customerArr = customerIds.split(",");
			for (String c : customerArr) {
				if (c != null && !"".equals(c)) {
					List<CustomerGroupDetail> existCustomerGroupDetailList = customerGroupDetailService.getByGroupId(id);
					boolean needAdd = false;
					if (existCustomerGroupDetailList == null) {
						needAdd = true;
					} else {
						if (!isExsitCustomer(existCustomerGroupDetailList, id)) {
							needAdd = true;
						}
					}
					if (needAdd) {
						index ++;
						customerGroupDetail.setCustomerGroup(customerGroup);
						Customer customer = customerService.get(Long.parseLong(c));
						customerGroupDetail.setCustomer(customer);
						if (!beanValidator(model, customerGroupDetail)){
							return form(id, customerGroupDetail, model);
						}
						customerGroupDetailService.save(customerGroupDetail);
					}
				}
			}
		}
		if (index > 0) {
			addMessage(redirectAttributes, customerGroup.getGroupName() + "群成功增加" + index + "位成员");
		} else {
			addMessage(redirectAttributes, customerGroup.getGroupName() + "群未增加成员");
		}
		return "redirect:"+Global.ADMIN_PATH+"/base/customerGroupDetail/list?groupId=" + id;
	}
	@RequestMapping(value = "delete")
	public String delete(@RequestParam("groupId") Long groupId, @RequestParam("customerId") Long customerId, Model model, RedirectAttributes redirectAttributes) {
		if (customerId == 0) {
			List<CustomerGroupDetail> list = customerGroupDetailService.getByGroupId(groupId);
			for (CustomerGroupDetail customerGroupDetail : list) {
				customerGroupDetailService.delete(customerGroupDetail);
			}
			addMessage(redirectAttributes, "成功将此客户移出所有成员");
			return "redirect:"+Global.ADMIN_PATH+"/base/customerGroupDetail/list?groupId=" + groupId;
		}
		CustomerGroupDetail cgd = customerGroupDetailService.getByPkId(groupId, customerId);
		if (cgd != null) {
			customerGroupDetailService.delete(cgd);
		}
		addMessage(redirectAttributes, "成功将此客户移出");
		return "redirect:"+Global.ADMIN_PATH+"/base/customerGroupDetail/list?groupId=" + groupId;
	}
	
	public boolean isExsitCustomer(List<CustomerGroupDetail> list, Long cid){
		for (CustomerGroupDetail customerGroupDetail : list) {
			if (customerGroupDetail.getCustomer().getId().equals(cid)) {
				return true;
			}
		}
		return false;
	}
}
